﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTV_V2.BussinessLogic;
using QLTV_V2.BussinessObject;
using QLTV_V2.DataAccess;

namespace QLTV_V2
{
    public partial class Frm_PhanQuyen : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public Frm_PhanQuyen()
        {
            InitializeComponent();
        }
        ConnectData conn = new ConnectData();
        Utilities uti = new Utilities();
        DanhMucBUS dmBUS = new DanhMucBUS();
        string sql;
        private void Frm_PhanQuyen_Load(object sender, EventArgs e)
        {
            dgv_Quyen.DataSource = uti.getDataSource("ChucNang", "TenCN");
            dgv_NSD.DataSource = uti.getDataSource("NhanVien", "MaNV");
            comb_GioiTinh.Items.Add("Nam");
            comb_GioiTinh.Items.Add("Nữ");
            comb_GioiTinh.SelectedIndex=0;
            radio_MaNV.Checked=true;
        }
        private string getSQL()
        {
            if (radio_MaNV.Checked == true)
                sql = "select * from NhanVien where MaNV like N'%"+txt_TimKiem.Text+"%' order by MaNV";
            else
                if (radio_TenNV.Checked == true)
                    sql = "select * from NhanVien where TenNV like N'%" + txt_TimKiem.Text+ "%' order by MaNV";
                else
                    sql = "select * from NhanVien order by MaNV";
            return sql;
        }

        private void btn_TimKiem_Click(object sender, EventArgs e)
        {
            dgv_NSD.DataSource = conn.GetDataTable(getSQL());
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            dgv_Quyen.CurrentCell = dgv_Quyen.Rows[0].Cells[0];
            int dem = 0;
            if (uti.CheckExitsValue("QuyenHan", "MaNV", txt_MaNV.Text) == false)//tồn tại
            {
                if (uti.deleteTable("QuyenHan", "MaNV", txt_MaNV.Text) == true) //xóa tất cả quyền hiện tại
                {
                    dgv_Quyen.Rows[0].Cells[0].Selected = true;
                    for (int i = 0; i < dgv_Quyen.Rows.Count - 1; i++)
                    {
                        string lastID = uti.GetLastID("QuyenHan", "IDQuyen", "IDQuyen", "0");
                        int nextID = Convert.ToInt32(lastID) + 1;
                        if (Convert.ToBoolean(dgv_Quyen.Rows[i].Cells["check"].Value) == true)
                        {
                            string macn = dgv_Quyen.Rows[i].Cells["MaCN"].Value.ToString();
                            if (dmBUS.insertQuyen(nextID, txt_MaNV.Text, macn) == true)
                                dem++;
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < dgv_Quyen.Rows.Count - 1; i++)
                {
                    string lastID = uti.GetLastID("QuyenHan", "IDQuyen", "IDQuyen", "0");
                    int nextID = Convert.ToInt32(lastID) + 1;
                    if (Convert.ToBoolean(dgv_Quyen.Rows[i].Cells["check"].Value) == true)
                    {
                        string macn = dgv_Quyen.Rows[i].Cells["MaCN"].Value.ToString();
                        if (dmBUS.insertQuyen(nextID, txt_MaNV.Text, macn) == true)
                            dem++;
                    }
                }
            }
            if (dem != 0)
            {
                MessageBox.Show("Thành công!","Thông báo");
                Frm_PhanQuyen_Load(sender, e);
            }
        }

        private void dataGridView_NhanVien_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            //Lấy dữ liệu ra textbox
            int dong = e.RowIndex;
            txt_MaNV.Text = dgv_NSD.Rows[dong].Cells["MaNV"].Value.ToString();
            txt_TenNV.Text = dgv_NSD.Rows[dong].Cells["TenNV"].Value.ToString();
            date_NgaySinh.Value = Convert.ToDateTime(dgv_NSD.Rows[dong].Cells["NgaySinh"].Value);
            if (Convert.ToInt32(dgv_NSD.Rows[dong].Cells["GioiTinh"].Value) == 1)
                comb_GioiTinh.SelectedItem = "Nam";
            else
                comb_GioiTinh.SelectedItem = "Nữ";
            //Reset check box trong datagridview
            int count = dgv_Quyen.Rows.Count - 1;
            for (int i = 0; i < count; i++)
            {
                dgv_Quyen.Rows[i].Cells["check"].Value = false; 
            }
            //Load quyen theo maNV
            string[] arrayString = conn.getArray("MaCN", "QuyenHan", "MaNV", txt_MaNV.Text,1);
            int n = arrayString.Length;//Lấy số phần tử mảng
            if (uti.CheckExitsValue("QuyenHan", "MaNV", txt_MaNV.Text) == false)
            {
                for (int i = 0; i < count; i++)
                {
                    string maNC = dgv_Quyen.Rows[i].Cells["MaCN"].Value.ToString();
                    if (uti.TimKiem(arrayString, n, maNC) == true)
                        dgv_Quyen.Rows[i].Cells["check"].Value = true;
                }
            }
        }

        private void dgv_NSD_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            uti.dgv_GetSTT_RowHeader(sender, e, dgv_NSD);
        }

        private void dgv_Quyen_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            uti.dgv_GetSTT_RowHeader(sender, e, dgv_Quyen);
        }

        private void btn_SelectAll_Click(object sender, EventArgs e)
        {
            int count = dgv_Quyen.Rows.Count - 1;
            for (int i = 0; i < count; i++)
                dgv_Quyen.Rows[i].Cells["check"].Value = true;
        }

        private void btn_Deselect_Click(object sender, EventArgs e)
        {
            int count = dgv_Quyen.Rows.Count - 1;
            for (int i = 0; i < count; i++)
                dgv_Quyen.Rows[i].Cells["check"].Value = false;
        } 

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

   
    }
}
